package org.jpass.dc.business.project.bean;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.jpass.dc.business.user.bean.UserVOBean;
import org.jpass.dc.common.bean.UUIDSupport;

@Entity
@Table(name = "t_project")
public class CProject extends UUIDSupport implements Serializable {
	
	private static final long serialVersionUID = 5810320169085740391L;

	/* 项目的名称   */
	private String projectName;
	
	/* 项目类型 ，例如java项目、c项目 */
	private String projectType;
	
	/* 创建项目的时间   */
	private Date createdDate;
	
	/* 状态为关闭还是打开 */
	private boolean status;
	
	/* 项目所属用户   */
	private UserVOBean user;
	
	public CProject() {}
	
	public CProject(String id, boolean status) {
		this.id = id;
		this.status = status;
	}
	
	public CProject(String id, String projectName, String projectType, Date createdDate) {
		this.id = id;
		this.projectName = projectName;
		this.projectType = projectType;
		this.createdDate = createdDate;
	}
	
	public CProject(String id, String projectName, String projectType, Date createdDate, boolean status) {
		this.id = id;
		this.projectName = projectName;
		this.projectType = projectType;
		this.createdDate = createdDate;
		this.status = status;
	}

	public CProject(String projectName, String projectType, Date createdDate, UserVOBean user) {
		this.projectName = projectName;
		this.projectType = projectType;
		this.createdDate = createdDate;
		this.user = user;
	}
	
	@Column(nullable = false, length=255)
	public String getProjectName() {
		return projectName;
	}
	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}
	
	@Column(nullable = false, length=10)
	public String getProjectType() {
		return projectType;
	}

	public void setProjectType(String projectType) {
		this.projectType = projectType;
	}

	@Column(nullable = false, updatable = false)
	public Date getCreatedDate() {
		return createdDate;
	}
	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}
	
	@Transient //次注解表示此方法不需要被持久化
    public String getCreatedDateAsString() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(createdDate);
    }
	
	@Column(nullable = false)
	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="userId", nullable=false, updatable=false)
	public UserVOBean getUser() {
		return user;
	}
	
	public void setUser(UserVOBean user) {
		this.user = user;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((projectName == null) ? 0 : projectName.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CProject other = (CProject) obj;
		if (projectName == null) {
			if (other.projectName != null)
				return false;
		} else if (!projectName.equals(other.projectName))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}

}
